<template>
	<view>
		<z-paging ref="paging" v-model="list" @query="queryList" :cell-height-mode="'dynamic'" loading-more-no-more-text='' :auto="false">
			<view slot="top">
				<u-navbar :placeholder="true" :autoBack="true" title="报警中心" :safeAreaInsetTop="true" @rightClick="deleteAll" bgColor='#f5f5f5'>
					<view slot="right">
						<image style="width: 25px;height: 25px;" src="@appstatic/message/message_icon_titlebar.png" mode=""></image>
					</view>
				</u-navbar>
				<u-tabs :list="tabList" @change="tabChange"  :scrollable="true" lineColor="#EA5404"></u-tabs>
			</view>
			<z-paging-cell v-for="(item,index) in list" :key="index" class="cell">
				<view class="">
					<view v-if="item.address" class="message">您的爱车【{{item.vehicleName}}】于{{item.alarmTime}}在{{item.address}}发生{{item.alarmName}}</view>
					<view v-else class="message">您的爱车【{{item.vehicleName}}】于{{item.alarmTime}}发生{{item.alarmName}}</view>
					<view class="time">{{item.alarmTime}}</view>
				</view>
			</z-paging-cell>
			<!-- <view class="bimg">
				<image style="width:100%;height:100%" src="@appstatic/message/message_icon_interact_background.png" mode=""></image>
			</view> -->
			<!-- <empty v-if='!list.length' text='暂无订单数据' /> -->
		</z-paging>

	</view>
</template>

<script>
import empty from '@components/empty/empty.vue'
const app = getApp();
const network = require("@/manager/network-manager.js");
const config = require("@/config.js");
const util = require("@utils/util.js");
export default {
	data() {
		return {
			tabList: [],
			currentMenuIndex:0,
			list:[],
			deviceId:'',
			alarmId:'',
			current:0,
			fromType:'message'
		}
	},
	onLoad: function(options) {
		//debugger
		if(options.deviceId){
			this.deviceId=options.deviceId
		}
		if (util.isLogin(getApp())) {
		   this.$fire.$on('warnMsg', function(){
				this.getTab()
		   });
		}
		if(options.from){
			this.fromType=options.from
		}
	},
	onShow: function(options) {
		this.getTab()
	},
	
	mounted() {
		// this.queryList()
	},
	methods: {
		//告警list
		 queryList(pageNo, pageSize) {
			var that=this
			console.log(that.alarmId)
			if(this.currentMenuIndex==0){//车辆故障报警
				var uri=app.globalData.config.interfaces.URL_ALARM_GETALARMINFOLIST
				var params={
					// deviceId:'2022080900000001',
					deviceId:that.deviceId,
					alarmId:that.alarmId,
					pageNum:pageNo,
					pageSize:pageSize
				}
				network.get(uri, params, 'form').then(res => {
					//debugger
					if(res.code==200){
					this.$refs.paging.complete(res.data.list);
					}else{
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					console.error(res)
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
				})
			}
		},
		//告警类型查询
		getTab:function(){
			var that=this
			return new Promise((resolve,reject)=>{
				
				const list=[]
				var uri=app.globalData.config.interfaces.URL_ALARM_GETALARMTYPELIST
				var params={
					// deviceId:'2022080900000001',
					deviceId:that.deviceId,
				}
				network.get(uri, params, 'form').then(res => {
					//debugger
					if(res.code==200){
						var length=res.data.length
						for(var i=0;i<length;i++){
							list.push({name:res.data[i].alarmName,id:res.data[i].alarmId})
							// list.push(res.data[i].alarmName)
							// list.push({name:res.data[i].alarmName})
						}
						console.log(list)
						that.tabList=list
						if(res.data.length>0){
							that.alarmId=res.data[0].alarmId
							this.$refs.paging.reload();
							resolve(res.data[0].alarmId)
						}
					}else{
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					console.error(res)
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
				});
			}).then(value=>{
				console.log(value)
				var uri=app.globalData.config.interfaces.URL_SYS_SENDMSG_UPDATE_LEVCMSGUSERREADY
				var params={
					// alarmId:value,
					// deviceId:'2022080900000001',
					// deviceId:that.deviceId,
					// isRead:1,
					isType:5
				}
				network.post(uri, params, 'json').then(res => {
					// debugger
					if(res.code==200){
						// that.$refs.paging.reload();
						if(that.fromType=='message'){
							//刷新前一页
							let pages = getCurrentPages();
							if(pages.length==2){
								let prePage = pages[pages.length - 2];
								// #ifdef H5
								prePage.refresh();
								// #endif
								
								// #ifdef APP-PLUS
								prePage.$vm.refresh();
								// #endif
							}
						}
						util.syncBadge(getApp())
					}else{
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					console.error(res)
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
				})
			})
		},
		tabChange(item){
			console.log(item)
			this.alarmId=item.id
			this.$refs.paging.reload();
			// this.readAll();
		},
		//一键清除
		deleteAll(){
			var uri=app.globalData.config.interfaces.URL_ALARM_DELETEALL
			var params={
				alarmId:this.alarmId,
				// deviceId:'2022080900000001',
				deviceId:this.deviceId,
				isRead:1,
				deleteFlag:1,
			}
			network.post(uri, params, 'json').then(res => {
				if(res.code==200){
					this.$refs.paging.reload();
				}else{
					uni.showToast({
						title: res.message,
						duration: 3000,
						icon: 'none'
					})
				}
			}).catch(res => {
				console.error(res)
				uni.showToast({
					title: res.errMsg,
					duration: 3000,
					icon: 'none'
				})
			})
		},
		//一键已读
		//  readAll(){
			
		// 	var that=this
		// 	// await this.getTab();
		// 	// console.log(that.alarmId)
			
		// 	var uri=app.globalData.config.interfaces.URL_ALARM_READALL
		// 	var params={
		// 		alarmId:that.alarmId,
		// 		// deviceId:'2022080900000001',
		// 		deviceId:that.deviceId,
		// 		isRead:1,
		// 	}
		// 	network.post(uri, params, 'json').then(res => {
		// 		if(res.code==200){
		// 			// that.$refs.paging.reload();
		// 			//刷新前一页
		// 			let pages = getCurrentPages();
		// 			if(pages.length==2){
		// 				let prePage = pages[pages.length - 2];
		// 				// #ifdef H5
		// 				prePage.refresh();
		// 				// #endif
						
		// 				// #ifdef APP-PLUS
		// 				prePage.$vm.refresh();
		// 				// #endif
		// 			}
		// 		}else{
		// 			uni.showToast({
		// 				title: res.message,
		// 				duration: 3000,
		// 				icon: 'none'
		// 			})
		// 		}
		// 	}).catch(res => {
		// 		console.error(res)
		// 		uni.showToast({
		// 			title: res.errMsg,
		// 			duration: 3000,
		// 			icon: 'none'
		// 		})
		// 	})
		// }
	}
}
</script>
<style>
	page {
		background: url('@appstatic/img/my/bg-img.jpg') no-repeat center center fixed;
		background-size: cover;
		box-sizing: border-box;
	}
</style>
<style>
.message {
	font-weight: 400;
	font-size: 14px;
	line-height: 21px;
	color: #000000;
}
.time {
	font-weight: 400;
	font-size: 13px;
	line-height: 15px;
	color: #ADB0AE;
}
.cell {
	padding-left: 20px;
	padding-top: 20px;
	padding-right: 20px;
}
.bimg {
	width: 235.71px;
	height: 209.06px;
	position: fixed;
	right:0px;
	bottom:60px
}
</style>
